from torch.utils.data import Dataset
from PIL import Image
import os


# 创建类
class Mydata(Dataset):
    # 路径初始化
    def __init__(self, root_dir, label_dir):
        self.root_dir = root_dir
        self.label_dir = label_dir
        self.path = os.path.join(self.root_dir, self.label_dir)
        self.img_path = os.listdir(self.path)

    # 迭代器
    def __getitem__(self, index):
        img_name = self.img_path[index]
        img_item_path = os.path.join(self.root_dir, self.label_dir, img_name)
        img = Image.open(img_item_path)
        label = self.label_dir
        return img, label

    # 输出数据图片总数
    def __len__(self):
        return len(self.img_path)

# 设置路径
root_dir = "./hymenoptera_data/hymenoptera_data/train/"
ants_label_dir = "ants_image"
bees_label_dir = "bees_image"
# 实例化
ants_dataset = Mydata(root_dir, ants_label_dir)
bees_dataset = Mydata(root_dir, bees_label_dir)

train_dataset = ants_dataset + bees_dataset
img, label = train_dataset[0]
print(label)

